package com.mfnote.util;

import java.io.File;
import java.util.List;

import com.baidu.pcs.BaiduPCSActionInfo;
import com.baidu.pcs.BaiduPCSClient;
import com.mfnote.view.R;
import com.mfnote.view.main.MainActivity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;

public class BaiduYunUtils {

	public static String filePath = Environment.getExternalStorageDirectory()
			.getAbsolutePath();
	public static String[] fileName = new String[] { "record", "image", 
			"notes", "hand" };
	/** 上传附件前先删除云端的同名文件 */
	public static void delete_YunFiles(final String FileName,final List<String> list){
				
		File file = new File(filePath + "/MFNote/" + FileName);
		if (file.isDirectory()) { // 如果path表示的是一个目录则返回true
			//Log.d("filepath", file.getAbsolutePath());
			File[] array = file.listFiles(); // 列出所有文件
			for (int j = 0; j < array.length; j++) {
				final File f = array[j];
				if (f.isFile()) {// FILE TYPE
					String name = f.getName();
					String targetFileName = "/apps/mfnote/" + FileName + "/" + name;
					list.add(targetFileName);
				}else {// FOLDER TYPE
						// folderScan(f.getAbsolutePath());
				}
			}
		}
			
    }
	
	/** 遍历内存卡MFNote下的所有文件获取总大小 */
	public static long fileTotalSize() {
		long fileSize = 0;
		long singleFileSize = 0;
		for(int i=0;i<fileName.length;i++){
			File file = new File(filePath + "/MFNote/" + fileName[i]);
			if (file.isDirectory()) { // 如果path表示的是一个目录则返回true
				File[] array = file.listFiles(); // 列出所有文件

				for (int j = 0; j < array.length; j++) {
					File f = array[j];
					if (f.isFile()) {// FILE TYPE
						singleFileSize = f.length();
					} else {// FOLDER TYPE
					// folderScan(f.getAbsolutePath());
					}
					fileSize = singleFileSize + fileSize;
				}
			}
		}
		Log.d("totleSize", fileSize+"");
		return fileSize;
	}

	/** 登录成功后创建文件夹mfnote和四个子文件夹 */
	public static void mkdir(final String mbOauth) {
		if (null != mbOauth) {
			Thread workThread = new Thread(new Runnable() {
				public void run() {
					BaiduPCSClient api = new BaiduPCSClient();
					api.setAccessToken(mbOauth);
					String[] path = new String[] { "/apps/mfnote",
							"/apps/mfnote/hand",
							"/apps/mfnote/image",
							"/apps/mfnote/notes",
							"/apps/mfnote/record" };

					for (int i = 0; i < path.length; i++) {
						final BaiduPCSActionInfo.PCSFileInfoResponse ret = api
								.makeDir(path[i]);
					}
				}
			});
			workThread.start();
		}
	}

	/** 判断是否联网   */
	public static boolean isNetwork() {
		  ConnectivityManager cManager=(ConnectivityManager)MainActivity.instance.getSystemService(Context.CONNECTIVITY_SERVICE);
		  NetworkInfo info = cManager.getActiveNetworkInfo(); 
		  if (info != null && info.isAvailable()){ 
		       //do something 
		       //能联网 
		        return true; 
		  }else{ 
		       //do something 
		       //不能联网 
		        return false; 
		  } 

	}

}
